package com.easygem.front.service.imp;

import com.easygem.common.error.SystemCodeEnum;
import com.easygem.common.error.SystemException;
import com.easygem.common.model.front.Industry;
import com.easygem.common.vo.front.IndustryVo;
import com.easygem.front.converter.IndustryConverter;
import com.easygem.front.mapper.IndustryMapper;
import com.easygem.front.service.IndustryService;
import com.easygem.front.service.base.AbstractServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

/**
 * @author 何维佳
 * @version 1.0
 * @className IndustryServiceImpl
 * @Description TODO
 * @date 2024/12/25 19:19
 */
@Service
public class IndustryServiceImpl extends AbstractServiceImpl<IndustryMapper, Industry,Integer> implements IndustryService {

    @Autowired
    private IndustryMapper industryMapper;

    @Override
    public List<Industry> findIndustryList(String accountName) {
//        //获得到登录后的用户信息，通用方式
//        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        return industryMapper.selectIndustry(accountName);
    }

    /**
     * 添加行业类别
     *
     * @param industryVo 行业类别对象
     */
    @Override
    public void add(IndustryVo industryVo) {
        industryVo.setCreateTime(new Timestamp(System.currentTimeMillis()));
        Industry industry = new Industry();
        //获得到登录后的用户信息，通用方式
        BeanUtils.copyProperties(industryVo,industry);
        industryMapper.insert(industry);
    }

    /**
     * 得到所有行业类别的所有信息
     *
     * @return 行业类别集合列表
     */
    @Override
    public List<IndustryVo> getIndustryAll(String accountName) {
        return IndustryConverter.converterToIndustryVoList(industryMapper.getIndustryList(accountName));
    }

    /**
     * 查询所有已启用的
     *
     * @return 用于下拉列表框
     */
    @Override
    public List<IndustryVo> getIndustryListByEnable(String accountName) {
        return IndustryConverter.converterToIndustryVoList(industryMapper.selectIndustry(accountName));
    }

    /**
     * 拿到需要编辑行业类别的信息
     *
     * @param id id
     * @return
     */
    @Override
    public IndustryVo edit(Integer id) throws SystemException {
        Industry industry = industryMapper.selectByPrimaryKey(id);
        if(industry==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"编辑的行业类别不存在");
        }
        return IndustryConverter.converterToIndustryVo(industry);
    }

    /**
     * 更新行业类别
     *
     * @param industryVo 行业类别对象
     */
    @Override
    public void update(Integer id,IndustryVo industryVo) throws SystemException {
        Industry industry1 = industryMapper.selectByPrimaryKey(id);
        if(industry1==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"编辑的行业类别不存在");
        }
        Industry industry = new Industry();
        BeanUtils.copyProperties(industryVo,industry);
        industry.setIndustryId(id);
        industryMapper.updateByPrimaryKeySelective(industry);
    }
}
